package com.zg.common.alter.autoconfigure;

import com.zg.common.alter.interceptor.AlertInterceptor;
import com.zg.common.alter.scheduler.AlertScheduler;
import org.springframework.boot.autoconfigure.condition.*;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.servlet.Servlet;

/**
 * <p>Description: </p>
 *
 * @author zhanfeng.liang
 * @since 2025/2/11 16:43
 */

@Configuration
@ConditionalOnWebApplication
@EnableConfigurationProperties(AlertProperties.class)
@ConditionalOnClass({Servlet.class, DispatcherServlet.class})
@EnableScheduling
//@Import(AlertWebMvcConfig.class)
//@ConditionalOnProperty(prefix = "http.alert", name = "enabled", havingValue = "true", matchIfMissing = false)
public class AlertAutoConfiguration {

    @Bean
    @ConditionalOnMissingBean
    public AlertInterceptor alertInterceptor(AlertProperties properties) {
        return new AlertInterceptor(properties);
    }

    @Bean
    public WebMvcConfigurer alertWebConfig(AlertInterceptor interceptor,
                                           AlertProperties properties) {
        return new AlertWebMvcConfig(interceptor,properties);
    }

//    @Bean
//    @ConditionalOnBean(JavaMailSender.class)
//    @ConditionalOnProperty(prefix = "http.alert.email", name = "enable")
//    public NotificationService emailNotificationService(JavaMailSender mailSender,
//                                                        AlertProperties properties) {
//        return new EmailNotificationService(mailSender, properties);
//    }

    @Bean
    public AlertScheduler alertScheduler(AlertInterceptor interceptor,AlertProperties alertProperties) {
        return new AlertScheduler(interceptor,alertProperties);
    }
}